package com.repu.pubmedspider.Config;


import com.google.common.collect.Lists;
import com.repu.pubmedspider.Utils.Constant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    // 接口版本号
    private final String version = "1.0";
    // 接口大标题
    private final String title = "SpringBoot工程";
    // 具体的描述
    private final String description = "API文档自动生成示例";
    // 服务说明url
    private final String termsOfServiceUrl = "http://www.kingeid.com";
    // licence
    private final String license = "MIT";
    // licnce url
    private final String licenseUrl = "https://mit-license.org/";
    // 接口作者联系方式
    private final Contact contact = new Contact("daiyizheng", "https://gitee.com/daiyizheng", "3879422398@qq.com");

    /**
     * 注册Bean实体
     * @return
     */

    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.repu.pubmedspider"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Lists.newArrayList(apiKey()));
    }

    /**
     * 构建Authorization验证key
     * @return
     */
    private ApiKey apiKey() {
        return new ApiKey(Constant.TOKEN_HEADER_STRING,Constant.TOKEN_HEADER_STRING,"header");//配置输入token的备注 TOKEN_HEADER_STRING = "Authorization"
    }

    /**
     * 构建API文档的详细信息方法
     * @return
     */
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title(title)
                .termsOfServiceUrl(termsOfServiceUrl)
                .description(description)
                .version(version)
                .license(license)
                .licenseUrl(licenseUrl)
                .contact(contact)
                .build();
    }
}
